Skip to content

#Fix 21316#21473

Open
pranavmanglik wants to merge 4 commits into
python:masterfrom
pranavmanglik:fix-21316
Open

#Fix 21316#21473
pranavmanglik wants to merge 4 commits into
python:masterfrom
pranavmanglik:fix-21316

Conversation

@pranavmanglik
Copy link
Copy Markdown
Contributor

@pranavmanglik pranavmanglik commented May 12, 2026

This change adds support for statically evaluable conditional fields in NamedTuple class syntax.

Example:

class NT(NamedTuple):
    x: int

    if sys.version_info >= (3, 12):
        y: str

Fixes #21316

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@pranavmanglik
Copy link
Copy Markdown
Contributor Author

This PR currently adds support for statically evaluable conditional fields in NamedTuple definitions.

It covers:

  • reachable/unreachable conditional branches
  • if / elif / else
  • nested conditionals
  • multiple fields inside branches
  • conditional default values
  • handling of unknown conditions

The implementation reuses existing reachability analysis via infer_condition_value.

I intentionally scoped this PR to NamedTuple support first to keep the change focused. Similar support for dataclasses and other semantic-transform-based constructs could potentially be added in follow-up work, and I’d be interested in contributing to those as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support conditional definitions in dataclasses, etc.

1 participant